import { useMemo } from 'react';

import { useLocation } from 'react-router-dom';

import router from '@/router';
import { getBreadcrumbFromRoute } from '@/router/tools';

/**
 * 获取当前路由的面包屑信息
 * @returns 面包屑数组
 */
export function useBreadcrumb() {
  const location = useLocation();

  const breadcrumbs = useMemo(() => {
    const routes = router.routes;
    return getBreadcrumbFromRoute(location.pathname, routes);
  }, [location.pathname]);

  // 获取当前页面标题（面包屑的最后一项）
  const currentTitle = useMemo(() => {
    return breadcrumbs[breadcrumbs.length - 1]?.title || '未知页面';
  }, [breadcrumbs]);

  // 获取完整的面包屑路径字符串
  const breadcrumbPath = useMemo(() => {
    return breadcrumbs.map(item => item.title).join(' / ');
  }, [breadcrumbs]);

  return {
    breadcrumbs,
    currentTitle,
    breadcrumbPath,
  };
}
